package com.wowudi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wowudi.entity.Meta;
import com.wowudi.dto.Result;
import com.wowudi.mapper.MetaMapper;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wowudi.mapper.MenuMapper;
import com.wowudi.entity.Menu;
import com.wowudi.service.MenuService;
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MetaMapper metaMapper;

    /**
    * Description: 返回所有顶级菜单以及下的子菜单<br/>
    * date: 2022-3-14<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result getMenu() {
        //获取所有顶级菜单项
        List<Map<String, Object>> maps = menuMapper.selectMaps(new QueryWrapper<Menu>().eq("parent_id",0));
        List<Map<String, Object>> menuMap = isChildren(addMeta(maps));
        Result result = new Result();
        result.setData(menuMap);
        return result;
    }

    /**
    * Description: 添加菜单<br/>
    * date: 2022-3-21<br/>
    * @author 贺同志<br/>
    */
    @Options(useGeneratedKeys = true,keyColumn = "meta_id",keyProperty = "meta.metaId")
    @Override
    public int addMenu(Menu menu) {
        int metaId = metaMapper.insert(menu.getMeta());
        menu.setMetaId(menu.getMeta().getMetaId());
        int menuId = menuMapper.insert(menu);
        return menuId+menuId==2?1:0;
    }


    /**
    * Description: 修改菜单<br/>
    * date: 2022-3-22<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int updateMenu(Menu menu) {
        int menuId = menuMapper.updateById(menu);
        int metaId = metaMapper.updateById(menu.getMeta());
        return menuId+metaId;
    }


    /**
    * Description: 删除菜单以及子菜单<br/>
    * date: 2022-3-22<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int deleteMenu(Menu menu) {
        int menu_id = menuMapper.delete(new QueryWrapper<Menu>().eq("id",menu.getId()));
        int meta_id = metaMapper.delete(new QueryWrapper<Meta>().eq("meta_id", menu.getMetaId()));
        if (menu.getChildren() != null){
            deleteMenu(menu.getChildren());
        }
        return menu_id+meta_id;
    }


    /**
    * Description: 根据id返回菜单<br/>
    * date: 2022-3-28<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Menu getMenuById(Integer id) {
        Menu menu = menuMapper.selectMenuById(id);
        return menu;
    }


    // 删除菜单中的子菜单
    public void deleteMenu(List<Menu> children){
        children.forEach(item -> {
            int menu_id = menuMapper.delete(new QueryWrapper<Menu>().eq("id",item.getId()));
            int meta_id = metaMapper.delete(new QueryWrapper<Meta>().eq("meta_id", item.getMetaId()));
            if (item.getChildren()!=null){
                deleteMenu(item.getChildren());
            }
        });
    }


    //获取所有顶级菜单项
    public List<Map<String, Object>> addMeta(List<Map<String, Object>> maps) {
        for (int i = 0; i < maps.size(); i++) {
            //单纯处理一下日期
            String updateTime = maps.get(i).get("updateTime").toString();
            String createTime = maps.get(i).get("createTime").toString();
            maps.get(i).put("updateTime", updateTime);
            maps.get(i).put("createTime", createTime);


            //添加顶级菜单项的附加数据
            Object metaId = maps.get(i).get("metaId");
            QueryWrapper<Meta> wrapper = new QueryWrapper<Meta>().eq("meta_id", metaId);
            List<Map<String, Object>> metaMap = metaMapper.selectMaps(wrapper);

            HashMap<String, Object> meta = new HashMap<>();
            for (int j = 0; j < metaMap.size(); j++) {
                Iterator<String> iterator = metaMap.get(j).keySet().iterator();
                while (iterator.hasNext()) {
                    String column = iterator.next();
                    meta.put(column, metaMap.get(j).get(column));
                }
            }
            maps.get(i).put("meta", meta);
        }
        return maps;
    }

    //是否有子菜单
    public List<Map<String, Object>> isChildren(List<Map<String, Object>> maps) {
        for (int i = 0; i < maps.size(); i++) {
            List<Map<String, Object>> mapss = menuMapper.selectMaps(null);
            for (Map<String, Object> objectMap : mapss) {

                int parentId = (int) objectMap.get("parentId");
                int id = (int) maps.get(i).get("id");
                if (parentId == id) {
                    QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("parent_id", id);
                    List<Map<String, Object>> childrenMap = menuMapper.selectMaps(queryWrapper);
                    for (int k = 0; k < childrenMap.size(); k++) {
                        String uTime = childrenMap.get(k).get("updateTime").toString();
                        String cTime = childrenMap.get(k).get("createTime").toString();
                        childrenMap.get(k).put("updateTime", uTime);
                        childrenMap.get(k).put("createTime", cTime);
                    }
                    maps.get(i).put("children", addMeta(childrenMap));
                }
            }
        }
        return maps;
    }

}
